
import numpy as np
import tensorflow as tf

# a = np.random.random([2, 3, 2])
# a = tf.constant(a, dtype=tf.float32)
# def get_t(per):
#     t = []
#     index = tf.argmax(per, 0)
#     for i in range(per.shape[1]):
#         t.append(per[index[i]][i])
#     return t
#

# per_c = []
# for per in range(a.shape[0]):
#     print(sess.run(a[per]))
#     t_ = get_t(a[per])
#     per_c.append(t_)
#     print(sess.run(per_c))

def MIL_loss(p_c, t_c):
    loss = t_c * tf.log(p_c) + (1 - t_c) * tf.log(1 - p_c)
    cross_entropy = -tf.reduce_sum(loss)
    return cross_entropy
    pass

# a = tf.constant(np.random.random([3, 2]), dtype=tf.float32)
# b = tf.constant(np.random.random([3, 2]), dtype=tf.float32)
# sess = tf.InteractiveSession()
# sess.run(tf.global_variables_initializer())
#
# print(sess.run(MIL_loss(b, a)))
#
ONEHOTNUM = 5
def compute_onehot(scores, classes):
    index = classes[np.where(scores > 4)]
    index = index.astype(int)
    out = np.zeros((index.size, ONEHOTNUM))
    # one-hot
    for row in range(out.shape[0]):
        out[row][index[row] - 1] = 1
    return out > 0

a = np.random.random_integers(1, 9, [3, 2])
b = np.random.random_integers(1, 2, [3, 2])
out = compute_onehot(a, b)
pass